class_name Brick
extends Node2D

@export var spawn_item: SpawnItem
@onready var bumpable: Node2D = $TriggerArea
@onready var bump_timer: Timer = $BumpTimer
@onready var sprite_2d = $Sprite2D

const RECT_ORIGIN := Rect2(16, 0, 16, 16)
const RECT_BARE := Rect2(32, 0, 16, 16) # 不带亮面
const RECT_BUMPED := Rect2(48, 0, 16, 16)
const BRICK_BROKEN = preload("res://sence/objects/brick_broken.tscn")

var bumped := false

enum SpawnItem{
	EMPTY,
	COIN,
	UPGRADE, # 人物升级
	LIFE, # 增加一条命
	STAR, # 无敌状态
	VINE,
}


func on_bumped(player: Player,broken := false):
	if bumpable.can_bump:
		bumpable.can_bump = false
		if spawn_item == SpawnItem.EMPTY and (player.curr_mode != player.Mode.SMALL or broken):
			# 砖块破碎效果
			var broken_brick = BRICK_BROKEN.instantiate()
			broken_brick.position = global_position
			owner.add_child(broken_brick)
			queue_free()
		elif spawn_item == SpawnItem.COIN:
			# 在指定时间内顶出足够多的金币
			if not bumped:
				bumped = true
				bump_timer.start()
			if bump_timer.is_stopped():
				sprite_2d.region_rect = RECT_BUMPED
			else:
				bumpable.do_bump(true)
				bumpable.do_spawn(self,spawn_item,player)
		else:
			# 其它道具要等顶完再生成
			if spawn_item != SpawnItem.EMPTY:
					#SoundManager.play_sfx("Vine" if spawn_item == SpawnItem.VINE else "UpgradeAppear")
					sprite_2d.region_rect = RECT_BUMPED
			var recover =  player.curr_mode == Player.Mode.SMALL && spawn_item == SpawnItem.EMPTY
			await bumpable.do_bump(recover)
			if spawn_item != SpawnItem.EMPTY:
					bumpable.do_spawn(self, spawn_item, player)
